package com.micromall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.base.BaseController;
import com.micromall.common.utils.PageInfo;
import com.micromall.model.AlRegion;
import com.micromall.service.IAlRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment}  前端控制器
 * </p>
 * @author Dante Fung
 * @since 2017-07-28
 */
@Controller
@RequestMapping("/alRegion")
public class AlRegionController extends BaseController {
    
    @Autowired private IAlRegionService alRegionService;
    
    @RequestMapping("/manager")
    public String manager() {
        return "admin/alRegion/alRegionList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(AlRegion alRegion, HttpServletRequest request) {
        EntityWrapper<AlRegion> ew = new EntityWrapper<AlRegion>();
        alRegion.setCompanyCode("CN");
        ew.setEntity(alRegion);
        List<AlRegion> alRegions = alRegionService.selectList(ew);
        Map mapvo = new HashMap();
        mapvo.put("status","200");
        mapvo.put("data",alRegions);
        mapvo.put("msg","请求成功");
        return mapvo;
    }

    @PostMapping("/dataGridNew")
    @ResponseBody
    public Object dataGridNew(AlRegion alRegion, HttpServletRequest request) {
        EntityWrapper<AlRegion> ew = new EntityWrapper<AlRegion>();
        ew.setEntity(alRegion);
        List<AlRegion> alRegions = alRegionService.selectList(ew);
        List list = new ArrayList();
        Map mapvo = null;
        for (AlRegion al : alRegions) {
            mapvo = new HashMap();
            mapvo.put("id", al.getRegionId());
            mapvo.put("text",al.getRegionName());
            mapvo.put("parentId", al.getParentRegionId());
            list.add(mapvo);
        }

        return list;
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "admin/alRegion/alRegionAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(AlRegion alRegion) {
        boolean b = alRegionService.insert(alRegion);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        AlRegion alRegion = new AlRegion();
        boolean b = alRegionService.updateById(alRegion);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        AlRegion alRegion = alRegionService.selectById(id);
        model.addAttribute("alRegion", alRegion);
        return "admin/alRegion/alRegionEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(AlRegion alRegion) {
        boolean b = alRegionService.updateById(alRegion);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
    
}
